package com.charger.chargingStation.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.charger.domain.Charger;
import com.charger.charger.service.IChargerService;
import com.charger.chargingStationCharger.domain.ChargingstationCharger;
import com.charger.chargingStationCharger.service.ChargingstationChargerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.IChargingstationService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 充电站点Controller
 *  Lilysi到此一游
 * @author charger1
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/site/chargingStation")
public class ChargingstationController extends BaseController
{
    @Autowired
    private IChargingstationService chargingstationService;

    @Autowired
    private ChargingstationChargerService chargingstationChargerService;

    @Autowired
    private IChargerService chargerService;

    /**
     * 查询充电站点列表
     */
    @PreAuthorize("@ss.hasPermi('site:chargingStation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Chargingstation chargingstation)
    {
        startPage();
        List<Chargingstation> list = chargingstationService.selectChargingstationList(chargingstation);
        for (Chargingstation item:list){
            ChargingstationCharger chargingstationCharger=new ChargingstationCharger();
            chargingstationCharger.setChargingstationId(item.getChargingstationId());
            QueryWrapper queryWrapper=new QueryWrapper<>();
            if(chargingstationCharger.getChargingstationId()!=null){
                queryWrapper.eq("chargingstation_id",chargingstationCharger.getChargingstationId());
            }
            List<ChargingstationCharger> list1 = chargingstationChargerService.list(queryWrapper);
            List<Charger> chargers = new ArrayList<>();
            List<Long> chargerIds = new ArrayList<>();
            for (ChargingstationCharger item1:list1){
                chargerIds.add(item1.getChargerId());
                Charger charger = chargerService.selectChargerByChargerId(item1.getChargerId());
                chargers.add(charger);
            }
            item.setChargers(chargers);
            item.setChargerId(chargerIds);
        }
        return getDataTable(list);
    }

    /**
     * 导出充电站点列表
     */
    @PreAuthorize("@ss.hasPermi('site:chargingStation:export')")
    @Log(title = "充电站点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Chargingstation chargingstation)
    {
        List<Chargingstation> list = chargingstationService.selectChargingstationList(chargingstation);
        ExcelUtil<Chargingstation> util = new ExcelUtil<Chargingstation>(Chargingstation.class);
        util.exportExcel(response, list, "充电站点数据");
    }

    /**
     * 获取充电站点详细信息
     */
    @PreAuthorize("@ss.hasPermi('site:chargingStation:query')")
    @GetMapping(value = "/{chargingstationId}")
    public AjaxResult getInfo(@PathVariable("chargingstationId") Long chargingstationId)
    {
        Chargingstation chargingstation=chargingstationService.selectChargingstationByChargingstationId(chargingstationId);
            ChargingstationCharger chargingstationCharger = new ChargingstationCharger();
            chargingstationCharger.setChargingstationId(chargingstation.getChargingstationId());
            QueryWrapper queryWrapper = new QueryWrapper<>();
            if (chargingstationCharger.getChargingstationId() != null) {
                queryWrapper.eq("chargingstation_id", chargingstationCharger.getChargingstationId());
            }
            List<ChargingstationCharger> list = chargingstationChargerService.list(queryWrapper);
            List<Charger> chargers = new ArrayList<>();
            List<Long> chargerIds = new ArrayList<>();
            for (ChargingstationCharger item : list) {
                chargerIds.add(item.getChargerId());
                Charger charger = chargerService.selectChargerByChargerId(item.getChargerId());
                chargers.add(charger);
            }
            chargingstation.setChargerId(chargerIds);
            chargingstation.setChargers(chargers);
//            Lilysi到此一游
        return AjaxResult.success(chargingstation);
    }

    /**
     * 新增充电站点
     */
    @PreAuthorize("@ss.hasPermi('site:chargingStation:add')")
    @Log(title = "充电站点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Chargingstation chargingstation)
    {
        toAjax(chargingstationService.insertChargingstation(chargingstation));
        Long id=chargingstation.getChargingstationId();
        for(Long i:chargingstation.getChargerId()){
            ChargingstationCharger chargingstationCharger=new ChargingstationCharger();
            chargingstationCharger.setChargerId(i);
            chargingstationCharger.setChargingstationId(id);
            chargingstationChargerService.save(chargingstationCharger);
        }
        return success("添加成功！");
    }

    //添加关联充电桩
    @PreAuthorize("@ss.hasPermi('site:chargingStation:insertCharger')")
    @Log(title = "充电站点", businessType = BusinessType.INSERT)
    @PostMapping("/insertCharger")
    public AjaxResult insert(@RequestBody Chargingstation chargingstation)
    {
        Long id=chargingstation.getChargingstationId();
        for(Long i:chargingstation.getChargerId()){
            ChargingstationCharger chargingstationCharger=new ChargingstationCharger();
            chargingstationCharger.setChargerId(i);
            chargingstationCharger.setChargingstationId(id);
            chargingstationChargerService.save(chargingstationCharger);
        }
        return success("添加成功！");
    }

    /**
     * 修改充电站点
     */
    @PreAuthorize("@ss.hasPermi('site:chargingStation:edit')")
    @Log(title = "充电站点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Chargingstation chargingstation)
    {
        toAjax(chargingstationService.updateChargingstation(chargingstation));
        Long id=chargingstation.getChargingstationId();
        QueryWrapper<ChargingstationCharger> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("chargingstation_id",id);
        chargingstationChargerService.remove(queryWrapper);

        for(Long i:chargingstation.getChargerId()){
            ChargingstationCharger chargingstationCharger=new ChargingstationCharger();
            chargingstationCharger.setChargerId(i);
            chargingstationCharger.setChargingstationId(id);
            chargingstationChargerService.save(chargingstationCharger);
        }
        return success("修改成功！");
    }

    /**
     * 删除充电站点
     */
    @PreAuthorize("@ss.hasPermi('site:chargingStation:remove')")
    @Log(title = "充电站点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{chargingstationIds}")
    public AjaxResult remove(@PathVariable Long[] chargingstationIds)
    {
        return toAjax(chargingstationService.deleteChargingstationByChargingstationIds(chargingstationIds));
    }
}
